//
//  YCNetworkManager.m
//  RobNewGrain
//
//  Created by 唐可雄 on 16/6/27.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "YCNetworkManager.h"

#import "AFNetworking.h"

static YCNetworkManager *service = nil;

@implementation YCNetworkManager

#pragma mark - Life Cycle

+ (instancetype)sharedInstance {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        service = [[YCNetworkManager alloc] init];
    });
    
    return service;
}

#pragma mark - Methods

- (void)accessServer:(NSString *)server byPost:(NSDictionary *)params block:(void (^)(NSError *, NSDictionary *))block {
    NSLog(@"%@ ---- %@", server, params);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:server parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 获取返回的数据
        NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        NSLog(@"%@ ---- %@", server, responseJson);
        
        // 返回数据给调用者
        block(nil, responseJson);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@ ---- %@", server, error);
        
        // 返回错误给调用者
        block([NSError errorWithDomain:@"网络请求失败" code:-1 userInfo:nil], nil);
    }];
}

- (void)accessServer:(NSString *)server byPost:(NSDictionary *)params accessoryImg:(NSDictionary *)accessoryParams block:(void (^)(NSError *, NSDictionary *))block {
    NSLog(@"%@ ---- %@", server, params);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:server parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSArray *keys = [accessoryParams allKeys];
        NSInteger count = keys.count;
        for (int i = 0; i < count; i++) {
            NSString *key = keys[i];
            NSData *data = [accessoryParams objectForKey:key];
            
            [formData appendPartWithFileData:data name:key fileName:@"img.jpg" mimeType:@"image/jpg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 获取返回的数据
        NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        NSLog(@"%@ ---- %@", server, responseJson);
        
        // 返回数据给调用者
        block(nil, responseJson);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@ ---- %@", server, error);
    
        // 返回错误给调用者
        block([NSError errorWithDomain:@"网络请求失败" code:-1 userInfo:nil], nil);
    }];
}

- (void)accessServer:(NSString *)server byGet:(NSDictionary *)params block:(void (^)(NSError *, NSDictionary *))block {
    NSLog(@"%@ ---- %@", server, params);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:server parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 获取返回的数据
        NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        NSLog(@"%@ ---- %@", server, responseJson);
        
        block(nil, responseJson);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@ ---- %@", server, error);
        
        // 返回错误给调用者
        block([NSError errorWithDomain:@"网络请求失败" code:-1 userInfo:nil], nil);
    }];
}

@end
